<?php
namespace Backend\Models\Sys;

use Backend\Models\Base;

class Menu extends Base
{

    public function initialize()
    {
        parent::initialize();
    }

    public function gets()
    {
        $query = 'SELECT s1.*,s2.`name` AS up_name FROM `sys_menu` AS s1 LEFT JOIN `sys_menu` AS s2 ON s1.up_id=s2.id ORDER BY `weight` DESC';
        try {
            $ret = $this->db()->query($query)->fetchAll();
            if ($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function get($id)
    {
        try {
            $ret = $this->db()->query('SELECT * FROM `sys_menu` WHERE `id` = :id LIMIT 1', ['id' => $id])->fetch();
            if ($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function ups($level, $top)
    {
        try {
            $ret = $this->db()->query('SELECT `id`,`name` FROM `sys_menu` WHERE `level`=:level AND `up_id`=:up_id AND `status`=1 ORDER BY `weight` DESC', [
                'level' => $level,
                'up_id' => $top
            ])->fetchAll();
            if ($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function tops()
    {
        try {
            $ret = $this->db()->query('SELECT `id`,`name` FROM `sys_menu` WHERE `level`=1 AND `status`=1 ORDER BY `weight` DESC')->fetchAll();
            if ($ret !== false && is_array($ret)) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function edit($entity)
    {
        try {
            return $this->db()->updateAsDict('sys_menu', $entity, sprintf('id=%d LIMIT 1', intval($entity['id'])));
        } catch (\PDOException $ex) {
            $this->errorLog('UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
            return false;
        }
    }

    public function add($menu)
    {
        try {
            if ($this->db()->insertAsDict('sys_menu', $menu)) {
                return $this->db()->lastInsertId();
            }
        } catch (\PDOException $ex) {
            $this->errorLog('INSERT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    public function getRoleMenus()
    {
        try {
            $ret = $this->db()->query('SELECT `id`, `name`, `url`, `icon`, `up_id`,`level`,`top` FROM `sys_menu` ORDER BY `weight` DESC, `id` ASC')->fetchAll();
            if ($ret !== false && is_array($ret)) {
                $out = [];
                foreach ($ret as $v) {
                    $out[$v['id']] = $v;
                }
                unset($ret);
                return $out;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}
